package com.ruoyi.cbs.approval.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cbs.approval.domain.vo.AddApprovalUserReqVO;
import com.ruoyi.cbs.approval.domain.vo.ApprovalUserRespVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cbs.approval.domain.ApprovalUser;
import com.ruoyi.cbs.approval.service.IApprovalUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审核人配置Controller
 * 
 * @author ruoyi
 * @date 2025-11-12
 */
@RestController
@RequestMapping("/approval/user")
public class ApprovalUserController extends BaseController
{
    @Autowired
    private IApprovalUserService approvalUserService;

    /**
     * 查询审核人配置列表
     */
    @PreAuthorize("@ss.hasPermi('approval:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApprovalUser approvalUser)
    {
        startPage();
        List<ApprovalUser> list = approvalUserService.selectApprovalUserList(approvalUser);
        return getDataTable(list);
    }

    /**
     * 导出审核人配置列表
     */
    @PreAuthorize("@ss.hasPermi('approval:user:export')")
    @Log(title = "审核人配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ApprovalUser approvalUser)
    {
        List<ApprovalUser> list = approvalUserService.selectApprovalUserList(approvalUser);
        ExcelUtil<ApprovalUser> util = new ExcelUtil<ApprovalUser>(ApprovalUser.class);
        util.exportExcel(response, list, "审核人配置数据");
    }

    /**
     * 获取审核人配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('approval:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(approvalUserService.selectApprovalUserById(id));
    }

    /**
     * 新增审核人配置
     */
    @PreAuthorize("@ss.hasPermi('approval:user:add')")
    @Log(title = "审核人配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApprovalUser approvalUser)
    {
        return toAjax(approvalUserService.insertApprovalUser(approvalUser));
    }

    /**
     * 修改审核人配置
     */
    @PreAuthorize("@ss.hasPermi('approval:user:edit')")
    @Log(title = "审核人配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApprovalUser approvalUser)
    {
        return toAjax(approvalUserService.updateApprovalUser(approvalUser));
    }

    /**
     * 删除审核人配置
     */
    @PreAuthorize("@ss.hasPermi('approval:user:remove')")
    @Log(title = "审核人配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(approvalUserService.deleteApprovalUserByIds(ids));
    }


    /**
     * 审核节点配置审核人
     */
    @PreAuthorize("@ss.hasPermi('approval:user:add')")
    @Log(title = "审核节点配置审核人", businessType = BusinessType.INSERT)
    @PostMapping("/addConfigUsers")
    public AjaxResult addConfigUsers(@RequestBody AddApprovalUserReqVO addApprovalUserReqVO)
    {
        return toAjax(approvalUserService.addApprovalUsers(addApprovalUserReqVO));
    }

    /**
     * 查询审核节点上的所有审核人
     */
    @PreAuthorize("@ss.hasPermi('approval:user:list')")
    @GetMapping("/getApprovalUserListByConfigId/{configId}")
    public TableDataInfo getApprovalUserListByConfigId(@PathVariable("configId") Long configId)
    {
        startPage();
        List<ApprovalUserRespVO> list = approvalUserService.selectApprovalUsersByConfigId(configId);
        return getDataTable(list);
    }

}
